﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing ;
using System.Collections ;

namespace XDesigner.AppFramework.WinForms
{
    public class ImageContainer : IDisposable
    {
        private static ImageContainer _Instance = null;

        public static ImageContainer Instance
        {
            get 
            {
                if (_Instance == null)
                {
                    _Instance = new ImageContainer();
                }
                return _Instance;
            }
        }

        public ImageContainer()
        {
        }

        private class ImageInfo
        {
            public int Index = -1;
            public ArrayList References = new ArrayList();
        }
        private Dictionary<Image, ImageInfo> _Images = new Dictionary<Image, ImageInfo>();

        public void Add( object typeOrInstance, Image img)
        {
            if (typeOrInstance == null)
            {
                throw new ArgumentNullException("typeOrInstance");
            }
            if (img == null)
            {
                throw new ArgumentNullException("img");
            }
            if (_Images.ContainsKey(img))
            {
                ImageInfo info = _Images[img];
                if (info.References.Contains(typeOrInstance) == false)
                {
                    info.References.Add(typeOrInstance);
                }
            }
            else
            {
                ImageInfo info = new ImageInfo();
                info.References.Add( typeOrInstance );
                _Images[img] = info;
            }
        }

        public void SetImageIndex(Image img , int index )
        {
            if (img == null)
            {
                throw new ArgumentNullException("img");
            }
            if (_Images.ContainsKey(img))
            {
                _Images[img].Index = index;
            }
            else
            {
                ImageInfo info = new ImageInfo();
                info.Index = index;
                _Images[img] = info;
            }
        }

        public int GetImageIndex(Image img)
        {
            if (img == null)
            {
                throw new ArgumentNullException("img");
            }
            if (_Images.ContainsKey(img))
            {
                return _Images[img].Index;
            }
            else
            {
                ImageInfo info = new ImageInfo();
                _Images[img] = info;
                return info.Index;
            }
        }

        public Image this[object typeOrInstance]
        {
            get
            {
                if (typeOrInstance == null)
                {
                    throw new ArgumentNullException("typeOrInstance");
                }
                else
                {
                    foreach (Image img in _Images.Keys)
                    {
                        if (_Images[img].References.Contains(typeOrInstance))
                        {
                            return img;
                        }
                    }
                }
                return null;
            }
            set
            {
                if (typeOrInstance == null)
                {
                    throw new ArgumentNullException("typeOrInstance");
                }
                if ( value == null)
                {
                    return;
                    //throw new ArgumentNullException("Image");
                }
                if (_Images.ContainsKey( value ))
                {
                    ImageInfo info = _Images[ value ];
                    if (info.References.Contains(typeOrInstance) == false)
                    {
                        info.References.Add(typeOrInstance);
                    }
                }
                else
                {
                    ImageInfo info = new ImageInfo();
                    info.References.Add(typeOrInstance);
                    _Images[value] = info;
                }
            }
        }

        public bool ContainsKey(object key)
        {
            foreach (ImageInfo info in this._Images.Values)
            {
                if (info.References != null)
                {
                    if (info.References.Contains(key))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool Contains(Image img)
        {
            return _Images.ContainsKey(img);
        }


        #region IDisposable 成员

        public void Dispose()
        {
            foreach (Image img in _Images.Keys)
            {
                img.Dispose();
            }
            _Images.Clear();
        }

        #endregion
    }
}
